package zhtt.cn;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

/**
 * Created by ${USER} on   ${DATE} ${TIME}
 **/
@SpringBootApplication
@ComponentScan({
        "zhtt.cn.controller"
})
public class Main {
    private static ConfigurableApplicationContext context;

    public static void main(String[] args) {
        context = SpringApplication.run(Main.class, args);
    }

    /**
     * 调用该方法即可完成应用重启
     */
    public static void restart() {
        ApplicationArguments args = context.getBean(ApplicationArguments.class);
        Thread thread = new Thread(() -> {
            context.close();
            context = SpringApplication.run(Main.class, args.getSourceArgs());
        });
        thread.setDaemon(false); // 必须将重启线程设置为user线程，防止close方法触发jvm关闭所有线程，导致重启失败（当唯一运行的线程都是守护进程线程时，Java虚拟机将退出）
        thread.start();
    }
}
